<?php

namespace common\components;
use backend\modules\home\models\Category;
use backend\modules\home\models\Taglist;
use backend\modules\home\models\Advertlist;


class Tools
{

    public static function getTopCate(){
        $cate = array();
        $cate = Category::find()
            ->select('title,id')
            ->where(['cid' => 0])
            ->orderBy([
            'sort' => SORT_DESC,
            'id' => SORT_ASC,])
            ->indexBy('id')
            ->column();
        return $cate;
    }

    public static function getCateInfo($id){
        return Category::findOne($id);
    }

    public static function getTagList(){
        $list = Taglist::find()->orderBy('id desc')->asArray()->all();
        return (count($list) > 0)?$list:array();
    }

    public static function tagToTxt($tag){
        $tagarr = explode('&',$tag);
        if(count($tagarr) > 0){
            return Taglist::find()
                ->select('cont')
                ->where([
                    'in', 'id', $tagarr
                ])
                ->asArray()
                ->all();
        }else{
            return array();
        }
    }

    public static function getAdvert(){
        $date = date('Y-m-d');
        $list = Advertlist::find()
            // 当前时间要小于结束时间
            ->andWhere(['>=','finish',$date])
            // 当前时间要大于起始时间
            ->andWhere(['<=','start',$date])
            ->asArray()
            ->all();
        $count = count($list);
        if($count > 1){
            $key = mt_rand(0,$count - 1);
            return $list[$key];
        } elseif($count == 1) {
            return $list[0];
        }else{
            return false;
        }
    }


	public static function getTree($data, $cid)
    {
        $tree = [];
        foreach($data as $k => $v)
        {
            if($v['cid'] == $cid)
            {        //父亲找到儿子
                $v['options'] = self::getTree($data, $v['id']);
                $tree[] = $v;
                //unset($data[$k]);
            }
        }
        return $tree;
    }


    public static function getIp() {
        //strcasecmp 比较两个字符，不区分大小写。返回0，>0，<0。
        if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $ip = getenv('REMOTE_ADDR');
        } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        $res =  preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
        return $res;

    }

    public static function get_random_code($length = 32, $mode = 0)
    {
        switch ($mode) {
            case '1':
                $str = '1234567890';
                break;
            case '2':
                $str = 'abcdefghijklmnopqrstuvwxyz';
                break;
            case '3':
                $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
                break;
            case '4':
                $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
                break;
            case '5':
                $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
                break;
            case '6':
                $str = 'abcdefghijklmnpqrstuvwxyz234567890';
                break;
            default:
                $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
                break;
        }
        $result = '';
        $l = strlen($str);
        for ($i = 0; $i < $length; $i++) {
            $result .= $str[mt_rand(0, $l - 1)];
        }
        return $result;
    }

    public static function wordTime($time)
    {
        $int = time() - $time;
        if ($int <= 5) {
            $str = sprintf('刚刚', $int);
        } elseif ($int < 60) {
            $str = sprintf('%d秒前', $int);
        } elseif ($int < 3600) {
            $str = sprintf('%d分钟前', floor($int / 60));
        } elseif ($int < 86400) {
            $str = sprintf('%d小时前', floor($int / 3600));
        } elseif ($int < 864000) {
            $str = sprintf('%d天前', floor($int / 86400));
        } else {
            $str = date('Y-m-d', $time);
        }
        return $str;
    }

}